﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using PhotoBook.Model.Entities;

namespace PhotoBook
{
    /// <summary>
    /// Interaction logic for CadastroAlbum.xaml
    /// </summary>
    public partial class CadastroAlbum : Window
    {
        #region Properties

        private Repositorio.RepositorioAlbum _RepositorioAlbum;
        private Repositorio.RepositorioCategoria _RepositorioCategory;
        private ObservableCollection<Album> _Albuns;
        
        public ObservableCollection<Album> ListaAlbuns
        {
            get { return _Albuns; }
        }


        #endregion

        #region Constructor

        public CadastroAlbum(Repositorio.RepositorioAlbum repositorioAlbum, Repositorio.RepositorioCategoria repositorioCategoria)
        {
            InitializeComponent();
            _RepositorioAlbum = repositorioAlbum;
            _RepositorioCategory = repositorioCategoria;
            _Albuns = new ObservableCollection<Album>(_RepositorioAlbum.Select());
            listViewAlbuns.ItemsSource = _Albuns;
        }

        #endregion

        #region Event Handlers

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IList<Categoria> categorias = _RepositorioCategory.Select();
            cmbCategory.ItemsSource = categorias;
            if (categorias.Count > 0)
                cmbCategory.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Album album = new Album();
            album.Descricao = txtDescricao.Text;
            album.CategoriaAlbum = (Categoria)cmbCategory.SelectedItem;
            try
            {
                _RepositorioAlbum.Insert(album);
                _Albuns.Add(album);
                listViewAlbuns.ItemsSource = _Albuns;
            }
            catch (Exception ex)
            {
                //TODO Write Log.
                System.Windows.Forms.MessageBox.Show("Nao foi possivel salvar o album.");
            }

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {

        }

        #endregion

    }
}
